package io.noties.markwon.plugin.typogram.span

import android.graphics.Canvas
import android.graphics.Paint
import androidx.annotation.ColorInt
import io.noties.markwon.base.image.AsyncDrawable
import io.noties.markwon.base.image.span.AsyncDrawableSpan
import io.noties.markwon.theme.MarkwonTheme
import timber.log.Timber

/**
 * @since 4.3.0
 */
open class TypogramAsyncDrawableSpan(
    theme: MarkwonTheme,
    override val drawable: AsyncDrawable,
    @param:ColorInt private val color: Int
) : AsyncDrawableSpan(theme, drawable, ALIGN_CENTER, false) {
    private var appliedTextColor: Boolean

    init {
        // if color is not 0 -> then no need to apply text color
        appliedTextColor = color != 0
    }

    override fun draw(
        canvas: Canvas,
        text: CharSequence,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {
        if (drawable.hasResult()) {
            Timber.d("typogram draw image bounds:${drawable.resultDrawable?.bounds}")
        }
        Timber.d("typogram draw hasResult:${drawable.hasResult()} start:$start,end:$end,x:$x,y:$y,top:$top,bottom:$bottom")
        super.draw(canvas, text, start, end, x, top, y, bottom, paint)
    }

    fun drawable(): AsyncDrawable {
        return drawable
    }

    @ColorInt
    fun color(): Int {
        return color
    }
}